<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\House;
use App\Models\City;
use App\Models\HouseImg;
use App\Models\HouseOwner;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;
use Illuminate\Http\Request;
use App\Models\Houseattr;

class HouseController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new House(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('house_name');
            $grid->column('house_area');
            $grid->column('house_type')->display(function ($v){
                if($v==0){
                    return '新房';
                }elseif ($v==1){
                    return '二手房';
                }else{
                    return '出售';
                }
            });
            $grid->column('house_address');
            $grid->column('house_direction')->display(function ($v){
                return (Houseattr::find($v))->name;
            });
            $grid->column('house_year')->display(function ($v){
                return (Houseattr::find($v))->name;
            });
            $grid->column('house_floor')->display(function ($v){
                return (Houseattr::find($v))->name;
            });
            $grid->column('house_room')->display(function ($v){
                return (Houseattr::find($v))->name;
            });
            $grid->column('house_img')->image();
            $grid->column('house_date_type')->display(function ($v){
                return (Houseattr::find($v))->name;
            });
            $grid->column('house_price_range')->display(function ($v){
                return (Houseattr::find($v))->name;
            });
            $grid->column('house_owner')->display(function ($v){
                return (HouseOwner::find($v))->name;
            });
            $grid->column('house_status')->select(['0'=>'待审核','1'=>'审核不通过','2'=>'审核通过','3'=>'未租/售','4'=>'已租/售']);
            $grid->column('house_property_type')->display(function ($v){
                return (Houseattr::find($v))->name;
            });
            $grid->column('created_at');

            $grid->filter(function (Grid\Filter $filter) {
                //房源租凭
                $filter->equal('house_type')->radio(['0'=>'新房','1'=>'二手房','2'=>'出租房']);
                //省市区搜索
                $filter->equal('house_province')->select(City::where('pid',0)->get()->pluck('name', 'id'))->load('house_city', '/city');
                $filter->equal('house_city')->select()->load('house_city', '/city');
                $filter->equal('house_region')->select();
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new House(), function (Show $show) {
            $show->field('id');
            $show->field('broker_id');
            $show->field('house_name');
            $show->field('house_area');
            $show->field('house_type');
            $show->field('house_province');
            $show->field('house_city');
            $show->field('house_region');
            $show->field('house_address');
            $show->field('longitude');
            $show->field('latitude');
            $show->field('house_direction');
            $show->field('house_build_area');
            $show->field('house_use_area');
            $show->field('house_year');
            $show->field('house_floor');
            $show->field('house_room');
            $show->field('house_img');
            $show->field('house_date_type');
            $show->field('house_price_range');
            $show->field('house_config');
            $show->field('house_label');
            $show->field('house_owner');
            $show->field('house_status');
            $show->field('house_desc');
            $show->field('house_property_type');
            $show->field('is_recommend');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new House(), function (Form $form) {

            $form->row(function ($form){
                $form->display('id');
                $form->text('house_name');
                $form->text('house_area');
                $form->radio('house_type')
                    ->options(['0'=>'新房','1'=>'二手房','2'=>'出租房'])
                    ->default(2);
            });

            // 三个显示在一行，不需要可以去掉row方法
            $form->row(function ($form){
                // 省份
                $form->width(4)->select('house_province')->options(City::where('pid',0)->get()->pluck('name', 'id'))->rules('required')->load('house_city', '/city');
                // 城市
                $form->width(4)->select('house_city')->load('house_region', '/city');
                // 区域
                $form->width(4)->select('house_region');
            });

            $form->row(function ($form){
                $form->text('house_address');
                //地图
                $form->map('latitude','longitude','经纬度');
                //房源朝向
                $form->select('house_direction')->options(Houseattr::getHouseattrData('house_direction'));
                $form->text('house_build_area');
                $form->text('house_use_area');
                //房源年代
                $form->select('house_year')->options(Houseattr::getHouseattrData('house_year'));
                //房源楼层
                $form->select('house_floor')->options(Houseattr::getHouseattrData('house_floor'));
                //房源几室几厅几卫
                $form->select('house_room')->options(Houseattr::getHouseattrData('house_room'));
                $form->image('house_img')->saveFullUrl()->disk('oss');
                //$form->multipleImage('urls','房源轮播图')->saveFullUrl()->disk('oss');
                //房源租期方式
                $form->select('house_date_type')->options(Houseattr::getHouseattrData('house_date_type'));
                //房源价格范围
                $form->select('house_price_range')->options(Houseattr::getHouseattrData('house_price_range'));
                //房源配置
                $form->multipleSelect('house_config')->options(Houseattr::getHouseattrData('house_config'));
                //房源标签
                $form->multipleSelect('house_label')->options(Houseattr::getHouseattrData('house_label'));
                $form->select('house_owner')->options(HouseOwner::all()->pluck('name', 'id'));
                $form->radio('house_status')
                    ->options(['0'=>'待审核','1'=>'审核不通过','2'=>'审核通过','3'=>'未租/售','4'=>'已租/售'])
                    ->default(0);
                $form->editor('house_desc');
                $form->radio('house_property_type')->options(Houseattr::getHouseattrData('house_property_type'));
                $form->radio('is_recommend')->options(['0'=>'否','1'=>'是'])->default(0);

                $form->display('created_at');
                $form->display('updated_at');
            });
        });
    }

    /**
     * @param Request $request
     * @return mixed
     * 获取子类数据
     */
    public function city(Request $request)
    {
        $provinceId = $request->get('q');

        return City::where('pid', $provinceId)->get(['id', 'name as text']);
    }
}
